package sort;

import java.util.Arrays;

/**
 * @author chengz
 * @description 快速排序抄写
 * @create 2020-11-11 14:00
 */
public class QuickSort_1 {

    public static void main(String[] args) {
        int[] arr = {5, 1, 2, 6, 4, 7, 9, 8, 0};
        new QuickSort_1().sort(arr);
        Arrays.stream(arr).forEach(System.out::println);


    }

    private void sort(int[] arr) {
        int length = arr.length;
        quickSort(arr, 0, length - 1 );
    }

    private void quickSort(int[] arr, int left, int right) {
        if(left>=right){
            return ;
        }
        int p = partition(arr,left,right);
        quickSort(arr,left,p-1);
        quickSort(arr,p+1,right);
    }

    private int partition(int[] arr, int left, int right) {
        int i= left;
        int comparad= arr[left];
        for (int j=left+1;j<=right;j++){
            if(comparad<arr[j]){
                //不做操作
            }else{
                i++;
                swap(arr,j,i);
            }
        }
        swap(arr,left,i);
        return i;
    }

    private void swap(int[] arr, int index1, int index2) {
        if (index1 == index2) {
            return;
        }
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }


}
